﻿using Framework.Core.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework.ExtensionsMods.Quartz.Model
{
	public class ApiResult
	{
		public int code { get; set; }
		public string message { get; set; }
		/// <summary>
		/// 如果data值为null，则忽略序列化将不会返回data字段
		/// </summary>
		[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
		public object result { get; set; }

		/// <summary>
		/// 初始化一个新创建的APIResult对象，使其表示一个空消息
		/// </summary>
		public ApiResult()
		{
		}

		/// <summary>
		/// 初始化一个新创建的 ApiResult 对象
		/// </summary>
		/// <param name="_code"></param>
		/// <param name="msg"></param>
		public ApiResult(int _code, string msg)
		{
			code = _code;
			message = msg;
		}

		/// <summary>
		/// 初始化一个新创建的 ApiResult 对象
		/// </summary>
		/// <param name="_code"></param>
		/// <param name="msg"></param>
		public ApiResult(int _code, string msg, object data)
		{
			code = _code;
			message = msg;
			if (data != null)
			{
				result = data;
			}
		}

		/// <summary>
		/// 返回成功消息
		/// </summary>
		/// <returns></returns>
		public ApiResult Success()
		{
			code = (int)ResultCode.SUCCESS;
			message = "success";
			return this;
		}

		/// <summary>
		/// 返回成功消息
		/// </summary>
		/// <param name="data">数据对象</param>
		/// <returns>成功消息</returns>
		public static ApiResult Success(object data) { return new ApiResult(200, "success", data); }

		/// <summary>
		/// 返回成功消息
		/// </summary>
		/// <param name="msg">返回内容</param>
		/// <returns>成功消息</returns>
		public static ApiResult Success(string msg) { return new ApiResult(200, msg, null); }

		/// <summary>
		/// 返回成功消息
		/// </summary>
		/// <param name="msg">返回内容</param>
		/// <param name="data">数据对象</param>
		/// <returns>成功消息</returns>
		public static ApiResult Success(string msg, object data) { return new ApiResult(200, msg, data); }

		/// <summary>
		/// 访问被拒
		/// </summary>
		/// <param name="apiResult"></param>
		/// <returns></returns>
		public ApiResult On401()
		{
			code = (int)ResultCode.DENY;
			message = "access denyed";
			return this;
		}
		public ApiResult Error(ResultCode resultCode, string msg = "")
		{
			code = (int)resultCode;
			message = msg;
			return this;
		}

		/// <summary>
		/// 返回失败消息
		/// </summary>
		/// <param name="code"></param>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static ApiResult Error(int code, string msg) { return new ApiResult(code, msg); }

		/// <summary>
		/// 返回失败消息
		/// </summary>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static ApiResult Error(string msg) { return new ApiResult((int)ResultCode.CUSTOM_ERROR, msg); }

		public override string ToString()
		{
			return $"msg={message},data={result}";
		}
	}

	public class ApiResult<T> : IActionResult
	{
		public int code { get; set; }
		public string message { get; set; }
		public T result { get; set; }

		public Task ExecuteResultAsync(ActionContext context)
		{
			HttpResponse response = context.HttpContext.Response;
			string json = string.Empty;
			if (this != null)
			{
				json = JsonConvert.SerializeObject(this);
			}
			response.Headers["content-type"] = "application/json; charset=utf-8";
			return Task.FromResult(response.WriteAsync(json));
		}
	}
}
